package cn.cloudplug.core.sdk_huanxin;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.hyphenate.chat.EMChatManager;
import com.hyphenate.chat.EMChatRoomManager;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMContactManager;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMOptions;

import java.util.Iterator;
import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by pc on 2018/2/1.
 */

public class HXSdk {
    //单例模式
    private final static class Holder{
        private final static HXSdk Instance=new HXSdk();
    }

    public static HXSdk getInstance(){return Holder.Instance;}

    //参数
    private Context appContext;

    //初始化
    public void init(Context context){
        appContext =context.getApplicationContext();
        initHyphenate();
    }

    private void initHyphenate() {
        EMOptions options = new EMOptions();
        // 默认添加好友时，是不需要验证的
        options.setAcceptInvitationAlways(true);
        // 是否自动将消息附件上传到环信服务器，默认为True是使用环信服务器上传下载，如果设为 false，需要开发者自己处理附件消息的上传和下载
        options.setAutoTransferMessageAttachments(true);
        // 是否自动下载附件类消息的缩略图等，默认为 true 这里和上边这个参数相关联
        options.setAutoDownloadThumbnail(true);

        int pid = android.os.Process.myPid();
        String processAppName = getAppName(pid);
        // 如果APP启用了远程的service，此application:onCreate会被调用2次
        // 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
        // 默认的APP会在以包名为默认的process name下运行，如果查到的process name不是APP的process name就立即返回

        if (processAppName == null ||!processAppName.equalsIgnoreCase(appContext.getPackageName())) {
            Log.e("app", "enter the service process!");

            // 则此application::onCreate 是被service 调用的，直接返回
            return;
        }

        //初始化
        EMClient.getInstance().init(appContext, options);
        //在做打包混淆时，关闭debug模式，避免消耗不必要的资源
        EMClient.getInstance().setDebugMode(true);
    }

    private String getAppName(int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) appContext.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = appContext.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName;
    }

    //获得环信对象
    public EMClient getEMClient(){
        return EMClient.getInstance();
    }

    //好友
    public EMContactManager getEMContactManager(){
        return EMClient.getInstance().contactManager();
    }

    //聊天室
    public EMChatRoomManager getEMChatroomManager(){
        return EMClient.getInstance().chatroomManager();
    }

    //消息
    public EMChatManager getEMChatManager(){
        return EMClient.getInstance().chatManager();
    }

    //群组
    public EMGroupManager getEMGroupManager(){
        return EMClient.getInstance().groupManager();
    }

}
